
import { Debtor } from "../../../../../typings/types/debtor";
import { changeRecordAPI } from "../../../../api/debtor";
import { ZCC } from "../../../../utils/BasePage";
import { formatNumber, validStr, zcc } from "../../../../utils/util";

interface PageData {
  companyId: string,
}

new ZCC.RefreshListPage<Debtor.ChangeRecordCell>({

  getOdiInfo: async function () {

    this.setRefreshing()
    wx.showLoading({ title: '' })
    try {
      console.log(this.data.page)
      const res = await changeRecordAPI(
        this.data.companyId,
        this.data.page,
      );
      if (res.content) {
        const list = res.content.list
        console.log(res)

        let items: Debtor.ChangeRecordCell[] = []
        if (list.length > 0) {
          const cur = this.data.page === 1 ? 0 : this.data.items.length
          items = list.map((item, index) => {
            const idx = cur + (index + 1)
            console.log(idx)
            return {
              title: validStr(item.projectName),
              time: validStr(item.changeDate),
              index: formatNumber(idx),
              content: { title: '变更前', val: zcc.split(item.beforeContent, ';').join('\n'), sectitle: '变更后', secval: zcc.split(item.afterContent, ';').join('\n') },
              isExpand: false,
              id: item.id
            }
          })
        }

        this.updateDatasWithNewItems(items, res.content.total, res.content.hasNextPage)

      }
    } finally {
      this.endRefreshing()
      wx.hideLoading()
    }
  },
  /**
   * 页面的初始数据
   */
  data: <PageData>{

    companyId: '',

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.setData({
      companyId: this.options.companyId
    })
    console.log(this.data)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getOdiInfo()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    if (this.resetPageIfNeed()) {
      this.getOdiInfo()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log('onReachBottom')
    this.getOdiInfo()
  },

})